<script setup lang="ts">
import { deleteRoleAPI, getRoleDetailAPI, getRoleListAPI, getRoleUserAPI, getTreeListAPI } from '@/apis/system';
import type { Role, RoleData, RoleUser } from '@/types/system';
import { ref } from 'vue';
import userIcon from '@/assets/user.svg';
import userIconActive from '@/assets/user-active.svg';
import { ElMessage, ElMessageBox, type TreeInstance } from 'element-plus';

  // 获取角色列表
  const roleList=ref<Role[]>([])

  const  getRoleList= async()=>{
   const res= await getRoleListAPI()
   roleList.value=res.data
   // 获取成员数量
   getRoleDetail(roleList.value[0].roleId)
   // 获取成员列表
   getRoleUser(roleList.value[0].roleId)
 


  }
  getRoleList()

  // 切换角色 点击角色激活交互
  const  activeIndex=ref(0)
  // 点击角色激活交互
  const changeRole=(inx:number,roleId:number)=>{
    activeIndex.value=inx
    // 获取角色详情
    getRoleDetail(roleId)
    // 获取成员列表
    getRoleUser(roleId)
  }
 // 3. 获取权限列表
const treeList = ref<RoleData[]>([])

const getTreeList = async () => {
  const res = await getTreeListAPI()
  treeList.value = res.data
  // console.log(treeList.value);
  
}
getTreeList()


// 获取角色详情

const  perms=ref<number[][]>([])
  const treeRef=ref<TreeInstance[]>([])
      // 成员数量
      const userTotal=ref(0)
const  getRoleDetail=async(roleId:number)=>{
  const res=await getRoleDetailAPI(roleId)
// 接收权限列表
  perms.value=res.data.perms
  // 接收成员数量
  userTotal.value=res.data.userTotal

  treeRef.value.forEach((tree:TreeInstance,index:number)=>{
       tree.setCheckedKeys(perms.value[index])
  })

}
// tab切换
const activeTab=ref<'member'|'true'>('true')
// 获取成员列表
const roleUserList=ref<RoleUser[]>([])
const  getRoleUser=async(roleId:number)=>{
  const res=await getRoleUserAPI(roleId)
  roleUserList.value=res.data.rows
}



// 删除角色
const deleteRole=async(roleId:number)=>{
// 弹窗
const confirm=await ElMessageBox.confirm('确定要删除吗？','提示',{type:'warning'}).catch(()=>{})
if(confirm){
  await deleteRoleAPI(roleId)
  ElMessage.success('删除成功')
  getRoleList()
}
}
</script>

<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div class="role-item" v-for="(item, index) in roleList" :key="item.roleId"
         :class="{active:activeIndex===index}" @click="changeRole(index,item.roleId)">
        <div class="role-info" >
          <img :src="index === activeIndex ?  userIconActive : userIcon" class="icon" />
          <span>{{ item.roleName }}</span>  
        </div>
        <div class="more">
          <!-- 更多 -->
          <el-dropdown>
    <span class="el-dropdown-link">
      <img src="@/assets/more.svg" class="icon" />
    </span>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item @click="$router.push(`/roleAdd/${item.roleId}`)">
          <el-button link>编辑角色</el-button>
        </el-dropdown-item>
        <el-dropdown-item>
          <el-button type="danger" link @click="deleteRole(item.roleId)">删除</el-button>
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>

        </div>
      </div> 
      <el-button class="addBtn" size="small" @click="$router.push('/roleAdd')">添加角色</el-button>
    </div>
   
    <div class="right-wrapper">
      <!-- 左侧权限  -->
      <el-tabs v-model="activeTab">
        <el-tab-pane label="功能权限" name="true"> 
      <div class="tree-wrapper">
        <div v-for="(item,index) in treeList" :key="item.id" class="tree-item">
          <div class="tree-title"> {{ item.title }} </div>
          <!-- 树形组件 -->
          <el-tree :data="item.children" 
          :props="{ label: 'title', disabled: () => true }"
           show-checkbox
           default-expand-all
           :default-checked-keys="perms[index]"
           node-key="id"
           ref="treeRef"
          
           />
        </div>
    
      </div>
        </el-tab-pane>
         <!--  右侧成员-->
        <el-tab-pane :label="`成员(${userTotal})`" name="member">
         <div class="user-wrapper"> 
          <el-table :data="roleUserList" width="100%">
            <el-table-column type="index" width="250" label="序号" />
            <el-table-column prop="name" label="员工姓名" />
            <el-table-column prop="userName" label="登录账号" />

          </el-table>
         </div>
         </el-tab-pane>
  </el-tabs>
    </div>
  </div>
</template>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;

  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;


      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
        width: 20px;
      }
    }

    .more {
      display: flex;
      align-items: center;
      .icon {
          width: 15px;
        }
    }

    .addBtn {
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;

        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper {
      padding: 20px;
    }
  }
}
</style>